All files / src/app/api/sort-results/[id]/finalize route.ts

100% Statements 10/10
100% Branches 2/2
100% Functions 1/1
100% Lines 9/9

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27                      13x 13x 13x 13x 13x 13x       11x 9x   2x      
import { NextRequest, NextResponse } from "next/server";
import { requireAuth } from "@/lib/auth";
import { SortResultService } from "@/server/services/SortResultService";
import { getDb } from "@/lib/db";
import { requireApiResultAccess } from "@/server/dal/collection-access";
import { errorResponse } from "@/server/api/http-errors";
 
export async function POST(
  _request: NextRequest,
  { params }: { params: Promise<{ id: string }> },
) {
  try {
    const { id } = await params;
    const user = await requireAuth();
    const db = getDb();
    const access = await requireApiResultAccess(db, id, user.id);
    if (access instanceof NextResponse) return access;
    // finalizeResult owns the state guard + the one transaction that both
    // finalizes the snapshot and completes the curator's open session
    // (spec §E) — never call the repo directly here.
    const result = await SortResultService.fromDb(db).finalizeResult(id);
    return NextResponse.json(result);
  } catch (error) {
    return errorResponse(error);
  }
}